package com.atguigu.flink03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/2/20
 * 该案例演示了从Kafka中读取数据
 *
 * 如果使用KafkaSource连接器去消费kafka主题数据，底层会帮助我们维护消费的偏移量，保证消费的精准一次
 */
public class Flink05_Source_Kafka {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.指定数据源 创建kafka消费者对象
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("hadoop102:9092")
                .setTopics("first")
                .setGroupId("test")
                //Flink提供了一个简单的字符串类型序列化和反序列化的类  SimpleStringSchema
                .setValueOnlyDeserializer(new SimpleStringSchema())
                // 从消费组提交的位点开始消费，不指定位点重置策略
                //.setStartingOffsets(OffsetsInitializer.committedOffsets())
                // 从消费组提交的位点开始消费，如果提交位点不存在，使用最早位点
                //.setStartingOffsets(OffsetsInitializer.committedOffsets(OffsetResetStrategy.EARLIEST))
                // 从时间戳大于等于指定时间戳（毫秒）的数据开始消费
                //.setStartingOffsets(OffsetsInitializer.timestamp(1657256176000L))
                // 从最早位点开始消费
                //.setStartingOffsets(OffsetsInitializer.earliest())
                // 从最末尾位点开始消费
                .setStartingOffsets(OffsetsInitializer.latest())
                //.setProperty(ConsumerConfig.ISOLATION_LEVEL_CONFIG,"read_committed")
                .build();
        //TODO 3.将消费到的数据  封装为流
        DataStreamSource<String> kafkaDS = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafka_source");

        kafkaDS.print();
        env.execute();
    }
}
